<?php
class EventsController extends AppController {

    var $name = 'Events';

    function index($event_time = null) {
        if (!$event_time || !in_array($event_time, array('upcoming', 'past'))) {
            $event_time = 'upcoming';
        }
        
        $current_time = date('Y-m-d H:i:s', time());
        
        $account_type = $this->Session->read('account_type');
        
        switch ($account_type) {
            case 'user':
                $this->paginate = array(
                    'Event' => array(
                        'limit' => 5
                    )
                );
                if ($event_time == 'upcoming') {
                    $search_params = array(
                        'Event.event_type' => 'public',
                        'Event.event_start >=' => $current_time
                    );
                } else {
                    $search_params = array(
                        'Event.event_type' => 'public',
                        'Event.event_start <' => $current_time
                    );
                }
                $events = $this->paginate('Event', $search_params);
                break;
            case 'host':
                $host = $this->Session->read('host');
                
                $this->paginate = array(
                    'Event' => array(
                        'limit' => 5
                    )
                );
                if ($event_time == 'upcoming') {
                    $search_params = array(
                        'Event.host_id' => $host['Host']['id'],
                        'Event.event_start >=' => $current_time
                    );
                } else {
                    $search_params = array(
                        'Event.host_id' => $host['Host']['id'],
                        'Event.event_start <' => $current_time
                    );
                }
                $events = $this->paginate('Event', $search_params);
                break;
            default:
                break;
        }
        
        $this->set('events', $events);
        $this->set('event_time', $event_time);
        
        $this->render('index_' . $account_type);
    }    

    function add() {
        if ($this->Session->read('account_type') != 'host') {
            $this->Session->setFlash('Access denied.');
            $this->redirect(array('action' => 'index'));
        }
        
        //if the form is submitted
        if(!empty($this->data)){
            $host = $this->Session->read('host');
            $this->data['Event']['host_id'] = $host['Host']['id'];
            if($this->Event->save($this->data)){
                $new_event_id = $this->Event->getLastInsertId();
                
                //Moving and renaming the image uploaded
                if($this->data['Event']['image']['tmp_name']){

                    $target_path = 'img/event/' . $new_event_id . '.jpg';

                    move_uploaded_file($this->data['Event']['image']['tmp_name'], $target_path);
                }
                
                $this->Session->setFlash('The event was successfully created.', 'default', array('class' => 'success'));
                $this->redirect(array('action' => 'index'));
            }
        }
    }
}

?>